package huanxin;

import cn.com.wishcloud.child.module.easemob.EasemobHelper;
import demo.JfinalConfig;
import org.apache.activemq.ActiveMQConnectionFactory;

import javax.jms.*;
import java.util.Enumeration;
import java.util.List;

/**
 * Created by ly on 2016/4/25.
 */
public class LocalHuanxinConsumer {

    public static final String Q_NAME = "Huanxin";
    public static final int CREATE_USER = 1;
    public static final int CREATE_GROUP = 2;
    public static final int CREATE_GROUP_AT_ALL = 3;
    public static final int ADD_USER_TO_GROUP = 4;
    public static final int CREATE_USERS = 5;

    //参数1：演示环境为http://localhost:8080/child_spring ，生产环境为http://http://www.wishcloud.com.cn/child_spring
    public static void main(String[] args) throws Exception {

        System.out.println("usage: jms.url jms.username jms.password");

        JfinalConfig.start();

        String jmsUrl = "tcp://localhost:61616";
        String username = null;
        String password = null;
        if (args.length >= 1) {
            jmsUrl = args[0];
        }
        if (args.length >= 2) {
            username = args[1];
        }
        if (args.length >= 3) {
            password = args[2];
        }


        ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(username, password, jmsUrl);
        Connection connection = connectionFactory.createConnection();
        connection.start();

        Session session = connection.createSession(Boolean.FALSE, Session.AUTO_ACKNOWLEDGE);
        Destination destination = session.createQueue(Q_NAME);
        MessageConsumer consumer = session.createConsumer(destination);
        consumer.setMessageListener(new MessageListener() {

            @Override
            public void onMessage(Message msg) {
                System.out.println("receive msg:" + msg);
                try {
                    handler((MapMessage) msg);

                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    public static void handler(MapMessage message) throws JMSException {

        Enumeration mapNames = message.getMapNames();
        while (mapNames.hasMoreElements()) {
            String k = mapNames.nextElement().toString();
            System.out.println(k + " => " + message.getObject(k));
        }

        switch (message.getInt("type")) {
            case CREATE_USER:
//                EasemobHelper.createUser(message.getLong("userId"));
                HuanxinConsumer.saveHuanxinId(message.getLong("userId"));
                break;
            case CREATE_USERS:
                List<Long> list = (List<Long>) message.getObject("userIds");
                EasemobHelper.createUsers(list.toArray(new Long[]{}));
                HuanxinConsumer.saveHuanxinIds(list);
                break;
            case CREATE_GROUP:
                List<Long> s = (List<Long>) message.getObject("userIds");
                String groupId = EasemobHelper.createGroup(message.getString("groupName"),
                        s.toArray(new Long[]{}));

                HuanxinConsumer.update(message.getLong("classId"), groupId);

                break;
            case CREATE_GROUP_AT_ALL:
                List<Long> ss = (List<Long>) message.getObject("userIds");
                String groupId2 = EasemobHelper.createGroupAtAll(message.getString("groupName"), ss.toArray(new Long[]{}));
                HuanxinConsumer.update(message.getLong("classId"), groupId2);
                break;
            case ADD_USER_TO_GROUP:
                EasemobHelper.addUserToGroup(message.getString("groupId"), message.getLong("userId"));
                break;
        }
    }

}
